// let url = 'https://micheliafigo.xyz/str.ce'
let url = 'https://static.micheliafigo.xyz/books/video.mp4'
// let url = 'https://static.micheliafigo.xyz/books/1581347142049.jpeg'

console.log(1, new Date() - 0)

fetch(url).then(
  response => {
    console.log(3, new Date() - 0)
    fnLoad(response)
  },
  err => {
    // console.log(err)
  }
)

console.log(2, new Date() - 0)
circleFn()

let fnLoad = async response => {
  let reader = response.body.getReader()

  let contentLength = response.headers.get('Content-Length')
  let valueLength = 0
  let chunks = []

  while (true) {
    const { done, value } = await reader.read()
    console.log('while', done)
    if (done) break

    valueLength += value.length
    chunks.push(value)
  }

  let position = 0
  let chunkAll = new Uint8Array(valueLength)
  for (let chunk of chunks) {
    chunkAll.set(chunk, position)
    position += chunk.length
  }

  console.log(4, new Date() - 0)

  // let result = new TextDecoder('utf-8').decode(chunkAll)
  // console.log(result)

  setTimeout(() => {
    clearTimeout(window.time)
  }, 1000)
}

function circleFn() {
  clearTimeout(window.time)
  window.time = setTimeout(() => {
    console.log('timeOut', new Date() - 0)
    circleFn()
  }, 100)
}

/**
 *
 */
